package com.eco.controller;

import com.alibaba.fastjson.JSONObject;
import com.eco.constants.Constants;
import com.eco.netty.GenericNettyClient;
import com.eco.netty.NettyClientConfig;
import com.eco.response.BaseResponse;
import com.eco.response.ResultTool;
import com.eco.schedule.LightScheduleTask;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@Data
public class LightController {


    private final NettyClientConfig nettyClientConfig;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    
    private final LightScheduleTask lightScheduleTask;;

    public LightController(LightScheduleTask lightScheduleTask, NettyClientConfig nettyClientConfig) {
        this.lightScheduleTask = lightScheduleTask;
        this.nettyClientConfig = nettyClientConfig;
    }



    @GetMapping({"/getLampStatus"})
    public BaseResponse<Boolean> getLampStatus(String site) {
        String lightStatus = this.stringRedisTemplate.opsForValue().get(Constants.SENSOR_REDIS_KEY+"_"+site);
        return ResultTool.success(Constants.SENSOR_ON.equals(lightStatus));
    }

    @GetMapping({"/order2Knd"})
    public BaseResponse<String> order2Knd(String site, String order) {
        site = StringUtils.trim(site);
        order = StringUtils.trim(order);
        if (StringUtils.isEmpty(site) || StringUtils.isEmpty(order)) {
            log.error("点位或指令参数为空");
            return ResultTool.fail("点位或指令参数为空");
        }
        //得到设备
        GenericNettyClient client = nettyClientConfig.getClientMap().get(site);
        if (Constants.KND_TURN_ON.equals(order)) {
            client.turnOn();
        } else {
            client.turnOff();
        }
        stringRedisTemplate.opsForValue().set(Constants.SENSOR_REDIS_KEY+"_"+site,"turnoff".equals(order)?Constants.SENSOR_OFF:Constants.SENSOR_ON);
        return ResultTool.success("ok");
    }


    @GetMapping("getLambScheduleSetting")
    public BaseResponse<JSONObject> getLambScheduleSetting() {
        JSONObject result = new JSONObject();
        String start = stringRedisTemplate.opsForValue().get(Constants.LIGHT_SCHEDULE_START_KEY);
        String stop = stringRedisTemplate.opsForValue().get(Constants.LIGHT_SCHEDULE_STOP_KEY);
        result.put("start",start);
        result.put("stop",stop);
        return ResultTool.success(result);
    }


    @GetMapping("updateLambScheduleSetting")
    public BaseResponse updateLambScheduleSetting(String start,String stop) {
        stringRedisTemplate.opsForValue().set(Constants.LIGHT_SCHEDULE_START_KEY,start);
        stringRedisTemplate.opsForValue().set(Constants.LIGHT_SCHEDULE_STOP_KEY,stop);
        String[] startSplit = start.split(":");
        String[] stopSplit = stop.split(":");

        //0 35 12 * * ?
        String startCron = startSplit[2]+" "+startSplit[1]+" "+startSplit[0]+" * * ?";
        String stopCron = stopSplit[2]+" "+stopSplit[1]+" "+stopSplit[0]+" * * ?";;
        lightScheduleTask.updateCronExpression(startCron,stopCron);
        return ResultTool.success("ok");
    }
}
